package com.cup.bddm.api.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cup.bddm.api.entity.StreetEntity;
import com.cup.bddm.api.mapper.MerchantEntityMapper;
import com.cup.bddm.api.mapper.StreetEntityMapper;
import com.cup.bddm.api.req.StreetVo;
import com.cup.bddm.util.StrUtil;

@Service("api-street-service")
public class StreetService {
	@Autowired
	StreetEntityMapper streetEntityMapper;

	@Autowired
	MerchantEntityMapper merchantEntityMapper;

	public List<StreetEntity> findByVo(StreetVo vo) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("street_cd", vo.getStreet_cd());
		pms.put("street_nm", vo.getStreet_nm());
		return streetEntityMapper.pageByPms(pms, vo.pager());
	}

	public int add(StreetVo vo) {
		StreetEntity entity = new StreetEntity();
		entity.setStreet_cd(vo.getStreet_cd());
		entity.setStreet_nm(vo.getStreet_nm());
		entity.setStreet_nm_index(StrUtil.indexEncode(vo.getStreet_nm()));
		entity.setStreet_addr(vo.getStreet_addr());
		entity.setCup_branch_ins_id_cd(vo.getCup_branch_ins_id_cd());
		entity.setCup_branch_nm(vo.getCup_branch_nm());
		entity.setService_mchnt_cd(vo.getService_mchnt_cd());
		entity.setService_mchnt_nm(vo.getService_mchnt_nm());
		entity.setMchnt_num(vo.getMchnt_num());
		entity.setMan_hour(vo.getMan_hour());
		entity.setCity(vo.getCity());
		entity.setIs_del(0);
		entity.setCreate_time(new Date());
		entity.setUpdate_time(new Date());
//		entity.setDel_time(vo.getDel_time());
		entity.setRow_key(StrUtil.getStreetRowKey(entity));
		return streetEntityMapper.save(entity);
	}

	public int setIsDel(StreetEntity entity) {
		entity.setIs_del(1);
		entity.setDel_time(new Date());
		return streetEntityMapper.updateById(entity);
	}

	public int modify(StreetEntity entity, StreetVo vo) {
        entity.setStreet_cd(vo.getStreet_cd());
        entity.setStreet_nm(vo.getStreet_nm());
		entity.setStreet_nm_index(StrUtil.indexEncode(vo.getStreet_nm()));
        entity.setStreet_addr(vo.getStreet_addr());
        entity.setCup_branch_ins_id_cd(vo.getCup_branch_ins_id_cd());
        entity.setCup_branch_nm(vo.getCup_branch_nm());
        entity.setService_mchnt_cd(vo.getService_mchnt_cd());
        entity.setService_mchnt_nm(vo.getService_mchnt_nm());
        entity.setMchnt_num(vo.getMchnt_num());
        entity.setMan_hour(vo.getMan_hour());
        entity.setCity(vo.getCity());
		entity.setUpdate_time(new Date());
		entity.setRow_key(StrUtil.getStreetRowKey(entity));
		return streetEntityMapper.updateById(entity);
	}

	public int delete(StreetEntity entity) {
		return streetEntityMapper.deleteById(entity.getId());
	}

	public String getNewStreetCd(String biz_street_cd) {
		String maxStreetCd = streetEntityMapper.getMaxStreetCd(biz_street_cd);
		if (maxStreetCd == null) {
			maxStreetCd = "0001";
		} else {
			maxStreetCd = String.format("%0" + 4 + "d", Integer.parseInt(maxStreetCd) + 1);
		}
		return biz_street_cd + maxStreetCd;
	}

	public List<StreetEntity> findByIns(String cup_branch_ins_id_cd) {
		return streetEntityMapper.listByIns(cup_branch_ins_id_cd);
	}

	public StreetEntity oneById(StreetVo vo, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("id", vo.getId());
		if (unDel) {
			pms.put("is_del", "0");
		}
		return streetEntityMapper.oneById(pms);
	}

	public StreetEntity oneByStreetCd(StreetVo vo, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("street_cd", vo.getStreet_cd());
		if (unDel) {
			pms.put("is_del", "0");
		}
		return streetEntityMapper.oneByStreetCd(pms);
	}

	public StreetEntity oneByStreetCdAndSrvMchntCd(StreetVo vo, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("street_cd", vo.getStreet_cd());
		pms.put("service_mchnt_cd", vo.getService_mchnt_cd());
		if (unDel) {
			pms.put("is_del", "0");
		}
		return streetEntityMapper.oneByStreetCd(pms);
	}

	public List<StreetEntity> listByStreetCd(StreetVo vo, boolean unDel) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", vo.getCup_branch_ins_id_cd());
		pms.put("street_cd", vo.getStreet_cd());
		if (unDel) {
			pms.put("is_del", "0");
		}
		return streetEntityMapper.listByStreetCd(pms);
	}

	public List<StreetEntity> matchByNm(String cup_branch_ins_id_cd, String nm) {
		Map<String, Object> pms = new HashMap<>();
		pms.put("cup_branch_ins_id_cd", cup_branch_ins_id_cd);
		pms.put("nm", nm);
		return streetEntityMapper.matchByNm(pms);
	}
}
